#include "tmjsonworker.h"
#include "tmglobalctrl.h"

TMJsonWorker::TMJsonWorker(QObject *parent) : QObject(parent)
{
    m_prTimer = new QTimer();
    connect(m_prTimer,SIGNAL(timeout()),this,SLOT(slot_getJsonState()));
//    m_prTimer->start(500);
}

void TMJsonWorker::slot_loadJsonFile(QString filePath)
{


    try{
        m_prxJson->Load(filePath.toUtf8().toStdString());
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMJsonWorker::slot_loadJsonByName(QString m_Name)
{
    try{
        m_prxJson->CallByName(m_Name.toUtf8().toStdString());
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMJsonWorker::slot_loadJsonByClass(QString m_Class)
{
    try{
        m_prxJson->CallByClass(m_Class.toUtf8().toStdString());
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMJsonWorker::slot_loadJsonByContent(QString m_Content)
{
    try{
        m_prxJson->CallByContent(m_Content.toUtf8().toStdString());
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMJsonWorker::slot_stop()
{
    try{
        m_prxJson->Stop();
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}

void TMJsonWorker::slot_getJsonList()
{
    QStringList m_data;
    std::vector<std::string> m_strList;
    try{
        m_prxJson->List(m_strList);
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
    for(int i = 0;i < m_strList.size();i++){
        std::string str = m_strList.at(i);
        m_data.append(str.c_str());
    }
    emit sig_jsonList(m_data);
}

void TMJsonWorker::slot_getJsonState()
{
    try{
        bool state = m_prxJson->IsPlaying();
        if(state){
            emit sig_jsonState(true);
        }else{
            emit sig_jsonState(false);
        }
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Error:"<<e.what();
    }
}
